/*   
 * 文件名：UserServiceImpl.java
 * 版权：〈Copy Ruantu〉
 * 描述：〈描述〉
 * 修改人：〈MengFanding〉
 * 修改时间：2017 上午10:57:34
 * 修改单号：〈修改单号〉
 * 修改内容：〈修改内容〉
 */

package com.ruantu.clothing.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruantu.clothing.dao.UserDao;
import com.ruantu.clothing.dao.UserRoleDao;
import com.ruantu.clothing.entity.User;
import com.ruantu.clothing.entity.UserRole;
import com.ruantu.clothing.service.UserService;

/**
 * UserServiceImpl
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private UserRoleDao dao;

    @Autowired
    private UserRoleDao urDao;
    /**
     * 添加用户
     */
    @Override
    public List<User> tianUser(User user, int[] array) {
        List<UserRole> userRoleList = new ArrayList<UserRole>();
        try {
            userDao.addUser(user);
            User u = userDao.getByUserName(user.getUserName());
            for (int i = 0; i < array.length; i++) {
                userRoleList.add(new UserRole(u.getId(), array[i]));
            }
            // 添加用户角色表
            dao.addUserRole(userRoleList);
            // 存搜索参数
            User uu = new User();
            uu.setUserName("%%");
            uu.setJgid(user.getJgid());
            return userDao.getAllUser(uu);
        } catch (Exception e) {
            throw new RuntimeException("添加用户失败", e);
        }

    }

    @Override
    public Map<String, Object> shanUser(int id, String jgid) {
        // 刪除用戶
        Map<String, Object> map = new HashMap<String, Object>();
        if (userDao.getOneUser(id).getState() == 0) {
            userDao.delUser(id);
            // 刪除該用戶角色
            urDao.delUserRole(id);
            // 存搜索参数
            User uu = new User();
            uu.setUserName("%%");
            uu.setJgid(jgid);
            List<User> userList = userDao.getAllUser(uu);
            map.put("userList", userList);
            map.put("code", 0);
        } else {

            map.put("code", 2);
        }

        return map;
    }

    /**
     * 修改用户隶属机构和角色用保存
     */
    @Override
    public List<User> gengJG(User user, int[] array) {
        List<UserRole> userRoleList = new ArrayList<UserRole>();
        userDao.updateJG(user);
        // 刪除該用戶角色
        urDao.delUserRole(user.getId());
        for (int i = 0; i < array.length; i++) {
            userRoleList.add(new UserRole(user.getId(), array[i]));
        }
        // 添加用户角色表
        dao.addUserRole(userRoleList);
        user.setUserName("%%");
        List<User> userList = userDao.getAllUser(user);

        return userList;
    }

    /**
     * 重置用户密码
     */
    @Override
    public int gengUser(User user) {

            return userDao.xiuUser(user);


    }

    /**
     * 查询用户
     */
    @Override
    public List<User> findAllUser(User user) {
        try {
            if (StringUtils.isNotBlank(user.getUserName())) {
                user.setUserName("%" + user.getUserName() + "%");
            }
            return userDao.getAllUser(user);
        } catch (Exception e) {
            throw new RuntimeException("查詢用戶失敗", e);
        }

    }

    /**
     * 修改密码用
     */
    @Override
    public int gengPassWord(User user) {

            return userDao.updatePassWord(user);

    }


    @Override
    public Set<String> getRoles(String userName) {
        return userDao.getRoles(userName);
    }

    @Override
    public Set<String> getPermissions(String userName) {
        return userDao.getPermissions(userName);
    }

    @Override
    public User login(String userName) {

            return userDao.getByUserName(userName);

    }

}
